/*
 * Copyright (C) 2013 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using UnityEngine;
using System.Collections;
using UnityEngine.SocialPlatforms;

public class PlayGameServicesPlatform : ISocialPlatform {
    public ILocalUser localUser { get { return mLocalUser; } }
    private PlayGameServicesLocalUser mLocalUser = new PlayGameServicesLocalUser();

    // dummy request code for launching intents whose return value we don't care about
    private const int DummyRequestCode = 6000;

    public void Authenticate(ILocalUser localUser, System.Action<bool> callback) {
        mLocalUser.Authenticate(callback);
    }

    public void LoadFriends(ILocalUser localUser, System.Action<bool> callback) {
        mLocalUser.LoadFriends(callback);
    }

    public void ShowAchievementsUI() {
        PlayGameServicesManager.Instance.QueueJob((activity, gamesClient) => {
            PlayGameServicesManager.LogD("Showing achievements UI. Getting intent.");
            AndroidJavaObject intent = gamesClient.Call<AndroidJavaObject>("getAchievementsIntent");
            PlayGameServicesManager.LogD("Launching intent to show achievements UI.");
            activity.Call("startActivityForResult", intent, DummyRequestCode);
            PlayGameServicesManager.LogD("Launched intent to show achievements UI.");
        });
    }

    public void ShowLeaderboardUI() {
        PlayGameServicesManager.Instance.QueueJob((activity, gamesClient) => {
            PlayGameServicesManager.LogD("Showing leaderboards UI. Getting intent.");
            AndroidJavaObject intent = gamesClient.Call<AndroidJavaObject>("getAllLeaderboardsIntent");
            PlayGameServicesManager.LogD("Launching intent to show leaderboards UI.");
            activity.Call("startActivityForResult", intent, DummyRequestCode);
            PlayGameServicesManager.LogD("Launched intent to show leaderboards UI.");
        });
    }

    public void ReportScore(long score, string board, System.Action<bool> callback) {
        PlayGameServicesManager.Instance.QueueJob((activity, gamesClient) => {
            PlayGameServicesManager.LogD("Submitting score " + score + " to board " + board);
            gamesClient.Call("submitScore", board, score);
            PlayGameServicesManager.LogD("Submitted score " + score + " to board " + board);
            callback.Invoke(true);
        });
    }

    public void ReportProgress(string id, double progress, System.Action<bool> callback) {
        // Note: this implementation does NOT currently support incremental achievements.
        // Doing so would require us to convert the progress from a percentage to an actual
        // number of steps to increment the achievement in Play Games, which would require
        // us to first load the achievement to see how many steps it has and how many have been
        // already achieved. This will be implemented in the future.
        PlayGameServicesManager.Instance.QueueJob((activity, gamesClient) => {
            PlayGameServicesManager.LogD("ReportProgress id " + id + ", progress " + progress);
            if (progress < 100.0) {
                // means "reveal"
                PlayGameServicesManager.LogD("Revealing achievement: " + id);
                gamesClient.Call("revealAchievement", id);
            }
            else {
                // means "unlock"
                PlayGameServicesManager.LogD("Unlocking achievement: " + id);
                gamesClient.Call("unlockAchievement", id);
            }
        });
    }

    public IAchievement CreateAchievement() { return null; }
    public ILeaderboard CreateLeaderboard() { return null; }
    public void LoadAchievementDescriptions(System.Action<IAchievementDescription[]> callback) {}
    public void LoadAchievements(System.Action<IAchievement[]> callback) {}
    public void LoadScores(string leaderboardId, System.Action<IScore[]> callback) {}
    public void LoadUsers(string[] userIds, System.Action<IUserProfile[]> callback) {}
    public void LoadScores(ILeaderboard leaderboard, System.Action<bool> callback) {}
    public bool GetLoading(ILeaderboard leaderboard) { return false; }

}
